<template>
  <div class="goods-comment">
    <div class="head">
      <div class="data">
        <p>
          <span>{{ commentInfo.salesCount }}</span
          ><span>人购买</span>
        </p>
        <p>
          <span>{{ commentInfo.praisePercent }}</span
          ><span>好评率</span>
        </p>
      </div>
      <div class="tags">
        <div class="dt">大家都在说：</div>
        <div class="dd">
          <a
            href="javascript:;"
            :class="{ active: antiveIndex === index }"
            @click="selectTag(item, index)"
            v-for="(item, index) in commentInfo.tags"
            :key="index"
            >{{ item.title }}（{{ item.tagCount }}）</a
          >
        </div>
      </div>
    </div>
    <div class="sort">
      <span>排序：</span>
      <a href="javascript:;" :class="{active:index===sortIndex}" v-for="(item,index) in sortArr" :key="index" @click="selecSort(item,index)">{{item.type}}</a>
    </div>
    <!-- 列表 -->
    <div class="list">
      <div class="item" v-for="item in list" :key="item.id">
        <div class="user">
          <img :src="item.member.avatar" alt="" />
          <span>{{ filterName(item.member.nickname) }}</span>
        </div>
        <div class="body">
          <div class="score">
            <i v-for="i in item.score" :key="i" class="iconfont icon-wjx01"></i>
            <i
              v-for="i in 5 - item.score"
              :key="i"
              class="iconfont icon-wjx02"
            ></i>
            <span class="attr">{{ filterSpecs(item.orderInfo.specs) }}</span>
          </div>
          <div class="text">{{ item.content }}</div>
          <!-- 图片预览组件 -->
          <GoodsCommentImage :pictures='item.pictures' v-if='item.pictures.length'/>
          <div class="time">
            <span>{{ item.createTime }}</span>
            <span class="zan"
              ><i class="iconfont icon-dianzan"></i>{{ item.praiseCount }}</span
            >
          </div>
        </div>
      </div>
    </div>
    <!-- 分页组件 -->
    <XtxPagination :total='counts' :pageSize='reqParams.pageSize' :currentPage='reqParams.page'  @current-change='currentChange'/>
  </div>
</template>

<script>
import { findGoodsCommentInfo, findGoodsCommentList } from '@/api/product'
import { useRoute } from 'vue-router'
import { ref, inject, reactive, watch } from 'vue'
import XtxPagination from '@/components/library/xtx-pagination.vue'
import GoodsCommentImage from '../goods-comment-image.vue'

export default {
  components: { XtxPagination, GoodsCommentImage },
  name: 'GoodsComment',
  setup () {
    // 改变父组件对象中的评论数
    const goodsInfo = inject('goodsInfo')
    const antiveIndex = ref(0)
    const commentInfo = ref({})
    const route = useRoute()
    // 获取评论头部信息
    findGoodsCommentInfo(route.params.id).then(res => {
      commentInfo.value = res.result
      const obj = {
        title: '全部评价',
        tagCount: commentInfo.value.evaluateCount
      }
      const obj2 = {
        title: '有图',
        tagCount: commentInfo.value.hasPictureCount
      }
      commentInfo.value.tags.unshift(obj2)
      commentInfo.value.tags.unshift(obj)
      goodsInfo.value.collectCount = obj.tagCount
    })
    // 获取评论列表信息
    // 筛选条件准备
    const reqParams = reactive({
      page: 1,
      pageSize: 10,
      hasPicture: null,
      tag: null,
      sortField: null
    })
    // 总条数
    const counts = ref(0)
    // 监听reqParams的变化，在筛选条件改变后重新发送请求
    watch(reqParams, () => {
      findGoodsCommentList(route.params.id, reqParams).then(res => {
        list.value = res.result.items
        counts.value = res.result.counts
      })
    }, { immediate: true })
    const list = ref([])

    // 过滤姓名
    const filterName = (name) => {
      return name.slice(0, 1) + '***' + name.slice(-1)
    }
    // 过滤商品属性
    const filterSpecs = (arr) => {
      return arr.map(v => `${v.name}：${v.nameValue}`).join('，')
    }
    // 选择标签
    const selectTag = (val, index) => {
      antiveIndex.value = index
      if (val.title === '全部评价') {
        reqParams.hasPicture = null
        reqParams.tag = null
      } else if (val.title === '有图') {
        reqParams.hasPicture = true
        reqParams.tag = null
      } else {
        reqParams.hasPicture = null
        reqParams.tag = val.title
      }
      reqParams.page = 1
    }
    // 自己定义默认-最新-最热的数据筛选
    const sortArr = [
      { type: '默认', sortField: null },
      { type: '最新', sortField: 'createTime' },
      { type: '最热', sortField: 'praiseCount' }
    ]
    const sortIndex = ref(0)
    const selecSort = (val, index) => {
      sortIndex.value = index
      reqParams.sortField = val.sortField
      reqParams.page = 1
    }
    // 从分页传过来的页码
    const currentChange = (i) => {
      reqParams.page = i
    }
    return { commentInfo, antiveIndex, list, filterName, filterSpecs, selectTag, selecSort, sortArr, sortIndex, currentChange, reqParams, counts }
  }
}
</script>

<style scoped lang="less">
.goods-comment {
  .list {
    padding: 0 20px;
    .item {
      display: flex;
      padding: 25px 10px;
      border-bottom: 1px solid #f5f5f5;
      .user {
        width: 160px;
        img {
          width: 40px;
          height: 40px;
          border-radius: 50%;
          overflow: hidden;
        }
        span {
          padding-left: 10px;
          color: #666;
        }
      }
      .body {
        flex: 1;
        .score {
          line-height: 40px;
          .iconfont {
            color: #ff9240;
            padding-right: 3px;
          }
          .attr {
            padding-left: 10px;
            color: #666;
          }
        }
      }
      .text {
        color: #666;
        line-height: 24px;
      }
      .time {
        color: #999;
        display: flex;
        justify-content: space-between;
        margin-top: 5px;
      }
    }
  }
  .head {
    display: flex;
    padding: 30px 0;
    .data {
      width: 340px;
      display: flex;
      padding: 20px;
      p {
        flex: 1;
        text-align: center;
        span {
          display: block;
          &:first-child {
            font-size: 32px;
            color: @priceColor;
          }
          &:last-child {
            color: #999;
          }
        }
      }
    }
    .tags {
      flex: 1;
      display: flex;
      border-left: 1px solid #f5f5f5;
      .dt {
        font-weight: bold;
        width: 100px;
        text-align: right;
        line-height: 42px;
      }
      .dd {
        flex: 1;
        display: flex;
        flex-wrap: wrap;
        > a {
          width: 132px;
          height: 42px;
          margin-left: 20px;
          margin-bottom: 20px;
          border-radius: 4px;
          border: 1px solid #e4e4e4;
          background: #f5f5f5;
          color: #999;
          text-align: center;
          line-height: 40px;
          &:hover {
            border-color: @xtxColor;
            background: lighten(@xtxColor, 50%);
            color: @xtxColor;
          }
          &.active {
            border-color: @xtxColor;
            background: @xtxColor;
            color: #fff;
          }
        }
      }
    }
  }
  .sort {
    height: 60px;
    line-height: 60px;
    border-top: 1px solid #f5f5f5;
    border-bottom: 1px solid #f5f5f5;
    margin: 0 20px;
    color: #666;
    > span {
      margin-left: 20px;
    }
    > a {
      margin-left: 30px;
      &.active,
      &:hover {
        color: @xtxColor;
      }
    }
  }
}
</style>
